package com.esunny.resourcevisualization.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.esunny.resourcevisualization.domain.entity.User;
import com.esunny.resourcevisualization.manage.UserManage;
import com.esunny.resourcevisualization.proxy.ZentaoSSOProxy;
import com.esunny.resourcevisualization.service.LoginService;
import com.esunny.resourcevisualization.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * @version 1.0.0.0
 * @ClassName UserServiceImpl
 * @Description 用户数据逻辑处理
 * @Author baochencong
 * @Date 2020/11/5 16:59
 */
@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserManage userManage;

    @Resource
    private LoginService loginService;

    @Override
    public List<User> getUserListByGroupName(String userGroupName) {
        return userManage.getUserListByGroupName(userGroupName);
    }

    @Override
    public void updateUserList() {
        List<String> nameList = getUsernameList();
        Map<String, String> paramMap = new HashMap<>(1);
        for (String username : nameList) {
            paramMap.put("username", username);
            loginService.updateUserInfo(paramMap);
        }
    }

    @Override
    public List<User> getUserList() {
        return userManage.getUserList();
    }

    @Override
    public User getUserInfoByUsername(String username) {
        return userManage.getUserByName(username);
    }

    /**
     * 获取禅道所有用户的用户名
     *
     * @return 禅道所有用户的用户名
     */
    private List<String> getUsernameList() {
        JSONObject userInfoList = ZentaoSSOProxy.post("/esgroup/list", new HashMap<>());
        JSONObject userListJSON = userInfoList.getJSONObject("data").getJSONObject("userList");
        Set<String> usernameList = userListJSON.keySet();
        return new ArrayList<>(usernameList);
    }


}
